// import Link from "next/link";
import moment from "moment";
import "./index.scss";

const dealList = (list) => {
	const obj = {};
	list.forEach(item => {
		const day = moment(item.yxdjTimeStart).format("YYYY/MM/DD").toString();
		if (!obj[day]) {
			obj[day] = [];
		}
		obj[day].push(item);
	});
	return obj;
};
const avePrice = prices => {
	if(prices.length === 0 || prices[0] === "") return "暂无价格";
	const allPrice = prices.reduce((pre, next) => Number(pre) + Number(next), 0);
	return parseInt(allPrice/ prices.length) + "元"; 
};

const DayBlock = (props) => {
	const { data } = props;
	return (
		<div className="item-block">
			{data.map(item => (
				<div className="item" key={item._id}>
					<h2 className="title">{item.title}</h2>
					<div className="info">
						<div className="number">预（现）售楼幢号: {item.houseNumber}</div>
						<div className="meta">
							<span className="price">{avePrice(item.prices)}</span>
						</div>
					</div>
				</div>
			))}
		</div>
	);
};


const HouseList = (props) => {
	const { data } = props;
	const listObj = dealList(data) || []; 
	return (
		<div className="house-list">
			{Object.keys(listObj).map(key => (
				<li className="house-item" key={key}>
					<div className="time">
						{key}
					</div>
					<DayBlock data={listObj[key]} />
				</li>
			))}
		</div>
	);
};

export default HouseList;